package com.tanhua.server.service;


import com.alibaba.fastjson.JSON;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.*;
import com.tanhua.model.mongo.UserReport;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SoulService {
    @DubboReference
    private QuestionnaireApi questionnaireApi;

    @DubboReference
    private SoulQuestionApi soulQuestionApi;

    @DubboReference
    private OptionApi optionApi;

    @DubboReference
    private UserReportApi userReportApi;

    @DubboReference
    private ReportApi reportApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    public List<QuestionnaireVo> questionnaire() {
        //拿到用户id
        Long userId = UserHolder.getUserId();
        String levelSimple = "初级";
        String levelMiddle = "中级";
        String levelHart = "高级";
        //随机拿到初级问卷
        Questionnaire questionnaireSimple = questionnaireApi.questionnaire(levelSimple);
        //随机拿到中级问卷
        Questionnaire questionnaireMiddle = questionnaireApi.questionnaire(levelMiddle);
        //随机初级高级问卷
        Questionnaire questionnaireHart = questionnaireApi.questionnaire(levelHart);

        //根据问卷拿到试题id
        Long questionSimpleId = questionnaireSimple.getId();
        Long questionMiddleId = questionnaireMiddle.getId();
        Long questionHartId = questionnaireHart.getId();
        //添加到集合中去
        List<Long> questionId = new ArrayList<>();
        questionId.add(questionSimpleId);
        questionId.add(questionMiddleId);
        questionId.add(questionHartId);
        //拿到试题
        //Long id
        Map<Long, SoulQuestion> soulQuestionMap = soulQuestionApi.question(questionId);
        Set<Long> longs = soulQuestionMap.keySet();
        //根据试题拿到选项
        List<Options> optionList = optionApi.option(longs);


        //构建返回的vo
        List<QuestionnaireVo> questionnaireVoList = new ArrayList<>();
        //赋值
        QuestionnaireVo questionnaireSimpleVo = new QuestionnaireVo();
        QuestionnaireVo questionnaireMiddleVo = new QuestionnaireVo();
        QuestionnaireVo questionnaireHartVo = new QuestionnaireVo();
        BeanUtils.copyProperties(questionnaireSimple, questionnaireSimpleVo);
        BeanUtils.copyProperties(questionnaireMiddle, questionnaireMiddleVo);
        BeanUtils.copyProperties(questionnaireHart, questionnaireHartVo);


        //id
        questionnaireSimpleVo.setId(questionnaireSimple.getId().toString());
        questionnaireMiddleVo.setId(questionnaireMiddle.getId().toString());
        questionnaireHartVo.setId(questionnaireHart.getId().toString());


        // Collection<SoulQuestionVo>
        Collection<SoulQuestionVo> questionnaireSimpleVoList = new ArrayList<>();
        Collection<SoulQuestionVo> questionnaireMiddleVoList = new ArrayList<>();
        Collection<SoulQuestionVo> questionnaireHartVoList = new ArrayList<>();

        //构建试题的map集合
        Map<Long, SoulQuestionVo> soulQuestionVoMap = new HashMap<>();
        //赋值
        for (Options option : optionList) {
            SoulQuestionVo soulQuestionVo = new SoulQuestionVo();
            OptionVo optionVo = new OptionVo();
            //根据选项拿到试题对象
            SoulQuestion soulQuestion = soulQuestionMap.get(option.getSoulId());
            BeanUtils.copyProperties(option, optionVo);
            //id
            optionVo.setId(option.getId().toString());
            optionVo.setOption(option.getText());

            //看集合中是否存在SoulQuestionVo对象
            SoulQuestionVo sqv = soulQuestionVoMap.get(soulQuestion.getId());
            if (sqv != null) {
                //存在,直接存
                sqv.getOptions().add(optionVo);
            } else {
                //不存在,先复制再添加
                BeanUtils.copyProperties(soulQuestion, soulQuestionVo);
                //id
                soulQuestionVo.setId(soulQuestion.getId().toString());
                soulQuestionVo.getOptions().add(optionVo);
                soulQuestionVoMap.put(Long.valueOf(soulQuestionVo.getId()), soulQuestionVo);
                //soulQuestionVo进集合
                if (soulQuestion.getQuestionnaireId().toString().equals(questionnaireSimpleVo.getId())) {
                    questionnaireSimpleVoList.add(soulQuestionVo);
                } else if (soulQuestion.getQuestionnaireId().toString().equals(questionnaireMiddleVo.getId())) {
                    questionnaireMiddleVoList.add(soulQuestionVo);
                } else if (soulQuestion.getQuestionnaireId().toString().equals(questionnaireHartVo.getId())) {
                    questionnaireHartVoList.add(soulQuestionVo);
                }
            }
        }
        //将集合set进Vo
        questionnaireSimpleVo.setQuestions(questionnaireSimpleVoList);
        questionnaireMiddleVo.setQuestions(questionnaireMiddleVoList);
        questionnaireHartVo.setQuestions(questionnaireHartVoList);

        //根据用户id拿到userReportList
        List<UserReport> userReportList = userReportApi.report(userId);
        //isLock
        //ReportId
        for (UserReport userReport : userReportList) {

            if (userReport.getQuestionnaireId() == questionSimpleId) {
                //做过初级题目
                //开放初级
                questionnaireSimpleVo.setIsLock(0);
                //将初级的报告id赋值
                questionnaireSimpleVo.setReportId(userReport.getReportId().toString());
                //开放中级
                questionnaireMiddleVo.setIsLock(0);
            } else if (userReport.getQuestionnaireId() == questionMiddleId) {
                //做过中级题目
                //将中级的报告id赋值
                questionnaireMiddleVo.setReportId(userReport.getReportId().toString());
                //开放高级
                questionnaireHartVo.setIsLock(0);
            } else if (userReport.getQuestionnaireId() == questionHartId) {
                //做过高级
                //将高级的报告id赋值
                questionnaireHartVo.setReportId(userReport.getReportId().toString());
            }
        }
        //确保初级开放
        questionnaireSimpleVo.setIsLock(0);
        //添加到返回的集合
        questionnaireVoList.add(questionnaireSimpleVo);
        questionnaireVoList.add(questionnaireMiddleVo);
        questionnaireVoList.add(questionnaireHartVo);
        return questionnaireVoList;
    }


    public String putQuestionnaire(ArrayList<Map<String, String>> list) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        List<Long> optionIds = new ArrayList<>();
        List<Long> questionIds = new ArrayList<>();
        //拿到所有的选项id
        for (Map<String, String> map : list) {
            String optionId = map.get("optionId");
            String questionId = map.get("questionId");
            optionIds.add(Long.valueOf(optionId));
            questionIds.add(Long.valueOf(questionId));
        }
        //根据id选项查选项
        List<Options> optionsList = optionApi.getScore(optionIds);

        int sum = 0;
        for (Options options : optionsList) {
            sum += options.getScore();
        }
        UserReport userReport = new UserReport();
        //拿到成绩,判断区间
        if (sum <= 20) {
            userReport.setReportId(1L);
        } else if (sum > 20 && sum <= 40) {
            userReport.setReportId(2L);
        } else if (sum > 40 && sum <= 60) {
            userReport.setReportId(3L);
        } else {
            userReport.setReportId(4L);
        }
        userReport.setUserId(userId);
        //QuestionnaireId
        Long QuestionnaireId = soulQuestionApi.getQuestionnaire(questionIds);
        userReport.setQuestionnaireId(QuestionnaireId);
        UserReport report = userReportApi.save(userReport);
        //将JSON字符串返回给前端
        return JSON.toJSONString(report.getReportId());
    }


    public ReportVo questionnaireResult(Long id) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //通过id查找详情
        Report report = reportApi.getById(id);

        ReportVo reportVo = new ReportVo();
        //赋值
        BeanUtils.copyProperties(report, reportVo);
        //获取维度
        Integer outward = report.getOutward();
        Integer decide = report.getDecide();
        Integer abstracted = report.getAbstracted();
        Integer logos = report.getLogos();

        //dimensionsList
        List<Dimensions> dimensionsList = new ArrayList<>();
        Dimensions dimensions = new Dimensions("外向", "" + outward + "%");
        Dimensions dimensions1 = new Dimensions("判断", "" + decide + "%");
        Dimensions dimensions2 = new Dimensions("抽象", "" + abstracted + "%");
        Dimensions dimensions3 = new Dimensions("理性", "" + logos + "%");
        dimensionsList.add(dimensions);
        dimensionsList.add(dimensions1);
        dimensionsList.add(dimensions2);
        dimensionsList.add(dimensions3);
        reportVo.setDimensions(dimensionsList);

        List<Long> userIds = userReportApi.getByReport(id, userId);

        List<SimilarYou> similarYouList = new ArrayList<>();
        HashMap<Long, UserInfo> userInfo = userInfoApi.getByIds(userIds);
        if (userInfo != null) {
            //similarYouLis
            for (UserInfo user : userInfo.values()) {
                SimilarYou similarYou = new SimilarYou();
                BeanUtils.copyProperties(user, similarYou);
                similarYouList.add(similarYou);
            }
        }
        reportVo.setSimilarYou(similarYouList);
        return reportVo;
    }
}
